/*
 * NativeFileText.java  0.4.0 / Nov 5, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Native File Text.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class NativeFileText implements NativeFilePart {

// Private Fields. -------------------------------------------------------------

  private final AbstractNativeFile nativeFile;
  private String text;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new NativeFileText object.
   *
   * @param nativeFile the native file
   * @param text the part's text
   */
  public NativeFileText(AbstractNativeFile nativeFile, String text) {
    if (nativeFile == null) {
      throw new IllegalArgumentException("Null native file");
    }
    this.nativeFile = nativeFile;
    this.text = text;
  }

// Protected Constructors. -----------------------------------------------------

  /**
   * Constructs new NativeFileText object.
   *
   * @param nativeFile the native file
   */
  protected NativeFileText(AbstractNativeFile nativeFile) {
    this(nativeFile, null);
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Returns text separator.
   *
   * @param nativeFile the native file
   *
   * @return text separator
   */
  public static NativeFileText getSeparator(AbstractNativeFile nativeFile) {
    return new NativeFileText(nativeFile,
                              NativeGenerator.INSTANCE.getNewLine());
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns part's content.
   *
   * @return part's text
   */
  protected String getText() {
    return text;
  }

  /**
   * Sets part's content.
   *
   * @param text part's text
   */
  protected void setText(String text) {
    this.text = text;
  }

// Protected Methods. ----------------------------------------------------------

  /**
   * Returns native file.
   *
   * @return native file
   */
  protected AbstractNativeFile getNativeFile() {
    return nativeFile;
  }

  /**
   * Stores part's content.
   *
   * @param pw the print writer
   *
   * @throws IOException if any I/O exception occurred
   */
  protected void storeContent(PrintWriter pw) throws IOException {
    // empty implementation
  }

// NativeFilePart Interface Implementation. ------------------------------------

  /**
   * {@inheritDoc}
   */
  public void store(PrintWriter pw) throws IOException {
    if (text == null) {
      storeContent(pw);
    } else {
      pw.print(text);
    }
  }

}
